<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <title>问卷调查DEMO</title>
    <link rel="stylesheet" href="/survey/vendor/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="/survey/dist/css/bootstrapValidator.css"/>
    <script type="text/javascript" src="/survey/vendor/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/survey/vendor/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/survey/dist/js/bootstrapValidator.js"></script>
    <script>
        $.prototype.serializeObject = function () {
            var a,o,h,i,e;
            a = this.serializeArray();
            o={};
            h=o.hasOwnProperty;
            for(i=0;i<a.length;i++){
                e=a[i];
                if(!h.call(o,e.name)){
                    o[e.name]=e.value;
                }else{
                    o[e.name]=o[e.name]+","+e.value;
                }
            }
            return o;
        }
    </script>
</head>
<body>
<#if msg??>
<div class="container">
    <h1>${msg}</h1>
</div>
<#else>
<div class="container">
        <div class="row">
            <!-- form: -->
            <section>
                <div class="col-lg-8 col-lg-offset-2">
                    <div class="page-header">
                        <h2>问卷调查</h2>
                    </div>

                    <form id="defaultForm" method="post" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-lg-3 control-label">昵称</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="name" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">性别</label>
                            <div class="col-lg-5">
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="sex" value="1" /> 男性
                                    </label>
                                </div>
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="sex" value="2" /> 女性
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">爱好</label>
                            <div class="col-lg-5">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="hobbys" value="game" /> 玩游戏
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="hobbys" value="bike" /> 骑车
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="hobbys" value="run" /> 跑步
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="hobbys" value="shop" /> 购物
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">对我们的建议与意见</label>
                            <div class="col-lg-5">
                                <textarea rows="4" class="form-control" name="advice"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">手机号</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="telephone" />
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-lg-9 col-lg-offset-3">
                                <button type="button" class="btn btn-primary" id="submit">提交</button>
                                <button type="button" class="btn btn-info" id="resetBtn">清空</button>
                            </div>
                        </div>
                    </form>
                </div>
            </section>
            <!-- :form -->
        </div>
    </div>
</#if>
</body>
<script type="text/javascript">
    $(document).ready(function() {

        $('#defaultForm').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '昵称不能为空'
                        }
                    }
                },
                sex: {
                    validators: {
                        notEmpty: {
                            message: '性别必须填写哦'
                        }
                    }
                },
                'hobbys': {
                    validators: {
                        notEmpty: {
                            message: '请至少选择一个爱好'
                        }
                    }
                },
                advice: {},
                telephone: {
                    validators: {
                        notEmpty: {
                            message: '手机号不能为空'
                        },
                        regexp: {
                            regexp: /^1[3|4|5|7|8][0-9]{9}$/,
                            message: '请填写符合规范的手机号'
                        },
                        remote: {
                            url: '/outer/show/checktelephone',
                            message: '该手机号已存在，请重新输入',
                            delay: 500,
                            type: 'POST'
                        }
                    }
                }
            }
        });

        // 表单校验
        $('#validateBtn').click(function() {
            $('#defaultForm').bootstrapValidator('validate');
        });

        $('#resetBtn').click(function() {
            $('#defaultForm').data('bootstrapValidator').resetForm(true);
        });

        $("#submit").click(function() {
            let form = $('#defaultForm');
            let formData = form.data('bootstrapValidator');
            //开启验证
            formData.validate();
            //是否通过校验
            if(formData.isValid()){
                $.ajax({
                    type: 'POST',
                    url: '/outer/show/add',
                    data: JSON.stringify(form.serializeObject()),
                    contentType: "application/json; charset=utf-8",
                    success: function(result){
                        if(result.status==1){
                            formData.resetForm(true);
                            alert(result.message);
                        }else{
                            alert(result.message);
                        }
                    }
                })

            }
        })
    });
</script>
</html>
